package cn.yuanqiao.archive.liyong.service.impl;

import cn.yuanqiao.archive.appManager.service.ITenantSysConfigService;
import cn.yuanqiao.archive.liyong.domain.YqConsultRegister;
import cn.yuanqiao.archive.liyong.domain.YqDaRegister;
import cn.yuanqiao.archive.liyong.mapper.YqConsultRegisterMapper;
import cn.yuanqiao.archive.liyong.mapper.YqDaRegisterMapper;
import cn.yuanqiao.archive.liyong.service.IYqConsultRegisterService;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.SecurityUtils;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 查档登记表Service业务层处理
 *
 * @ProjectName: cn.yuanqiao.archive.liyong.service.impl
 * @ClassName: YqConsultRegisterServiceImpl
 * @Author: lenovo
 * @Description: hbWang
 * @Date: 2024/1/26 15:51
 * @Version: 1.0
 **/
@Service
public class YqConsultRegisterServiceImpl implements IYqConsultRegisterService {

    @Autowired
    private YqConsultRegisterMapper yqConsultRegisterMapper;

    @Autowired
    private YqDaRegisterMapper yqDaRegisterMapper;

    @Autowired
    private ITenantSysConfigService sysConfigService;

    /**
     * 查询查档登记表
     *
     * @param id 查档登记表主键
     * @return 查档登记表
     */
    @Override
    public YqConsultRegister selectYqConsultRegisterById(String id) {
        List<YqDaRegister> yqDaRegisters = yqDaRegisterMapper.selectYqDaRegisterCuId(id, new YqDaRegister().getTableName());
        YqConsultRegister yqConsultRegister = yqConsultRegisterMapper.selectYqConsultRegisterById(id, new YqConsultRegister().getTableName());
        yqConsultRegister.setYqDaRegisters(yqDaRegisters);
        return yqConsultRegister;
    }

    /**
     * 查询查档登记表列表
     *
     * @param yqConsultRegister 查档登记表
     * @return 查档登记表
     */
    @Override
    public List<YqConsultRegister> selectYqConsultRegisterList(YqConsultRegister yqConsultRegister) {
        return yqConsultRegisterMapper.selectYqConsultRegisterList(yqConsultRegister, yqConsultRegister.getTableName());
    }

    /**
     * 查询登记档案信息列表
     *
     * @param yqDaRegister 登记档案信息
     * @return 登记档案信息
     */
    @Override
    public List<YqDaRegister> selectYqDaRegisterList(YqDaRegister yqDaRegister) {
        if(StringUtils.isBlank( yqDaRegister.getConsultId())){
            yqDaRegister.setConsultId("1");
        }
        yqDaRegister.setUserId(String.valueOf(SecurityUtils.getUserId()));
        return yqDaRegisterMapper.selectYqDaRegisterList(yqDaRegister, yqDaRegister.getTableName());
    }


    /**
     * 新增查档登记表
     *
     * @param yqConsultRegister 查档登记表
     * @return 结果
     */
    @Override
    public int insertYqConsultRegister(YqConsultRegister yqConsultRegister) {
        String id = String.valueOf(SnowflakeIdGenerator.generateId());
        yqConsultRegister.setId(id);
        yqConsultRegister.setCreateTime(DateUtils.getNowDate());
        yqConsultRegister.setCreatorId(String.valueOf(SecurityUtils.getUserId()));
        yqDaRegisterMapper.updateCuID(id, yqConsultRegister.getCuIds().split(","), new YqDaRegister().getTableName());
        return yqConsultRegisterMapper.insertYqConsultRegister(yqConsultRegister, yqConsultRegister.getTableName());
    }

    /**
     * 添加到档案信息
     *
     * @param yqDaRegister
     * @return
     */
    @Override
    public int insertYqDaRegister(YqDaRegister yqDaRegister) {
        YqDaRegister yqDaRegisters = yqDaRegisterMapper.selectByDaId(yqDaRegister.getDaId(),String.valueOf(SecurityUtils.getUserId()), yqDaRegister.getTableName());
        if (yqDaRegisters != null) {
            throw new ServiceException("请勿重复登记同一条档案");
        }
        String id = String.valueOf(SnowflakeIdGenerator.generateId());
//        String s = yqDaRegister.getDaMsg().replaceAll("<span style='color: red;'>", "").replaceAll("</span>", "");
        String s = new Gson().toJson(yqDaRegisterMapper.selectDaByDaid(yqDaRegister.getSubTableName(), yqDaRegister.getDaId()));
        int startIndex = yqDaRegister.getDaMsg().indexOf("\"categoryId\":\"");
        int endIndex = yqDaRegister.getDaMsg().indexOf("\"", startIndex+"\"categoryId\":\"".length())+1;
        String categoryId = yqDaRegister.getDaMsg().substring(startIndex, endIndex);
        int lastCurlyBraceIndex = s.lastIndexOf('}');
        if (lastCurlyBraceIndex  != -1) {
            s = s.substring(0, lastCurlyBraceIndex) + ",\"SUB_TABLE_NAME\":\"" + yqDaRegister.getSubTableName() + "\"," + categoryId + s.substring(lastCurlyBraceIndex);
        }
        yqDaRegister.setId(id);
        yqDaRegister.setConsultId("1");
        yqDaRegister.setUserId(String.valueOf(SecurityUtils.getUserId()));
        yqDaRegister.setDaMsg(s);
        return yqDaRegisterMapper.insertYqDaRegister(yqDaRegister, new YqDaRegister().getTableName());
    }

    /**
     * 修改查档登记表
     *
     * @param yqConsultRegister 查档登记表
     * @return 结果
     */
    @Override
    public int updateYqConsultRegister(YqConsultRegister yqConsultRegister) {
        return yqConsultRegisterMapper.updateYqConsultRegister(yqConsultRegister, yqConsultRegister.getTableName());
    }

    /**
     * 批量删除查档登记表
     *
     * @param ids 需要删除的查档登记表主键
     * @return 结果
     */
    @Override
    public int deleteYqConsultRegisterByIds(String[] ids) {
        return yqConsultRegisterMapper.deleteYqConsultRegisterByIds(ids, new YqConsultRegister().getTableName());
    }

    /**
     * 删除查档登记表信息
     *
     * @param id 查档登记表主键
     * @return 结果
     */
    @Override
    public int deleteYqConsultRegisterById(String id) {
        return yqConsultRegisterMapper.deleteYqConsultRegisterById(id, new YqConsultRegister().getTableName());
    }

    /**
     * 批量删除登记档案信息
     *
     * @param ids 需要删除的登记档案信息主键
     * @return 结果
     */
    @Override
    public int deleteYqDaRegisterByIds(String[] ids) {
        return yqDaRegisterMapper.deleteYqDaRegisterByIds(ids, new YqDaRegister().getTableName());
    }

    /**
     * 预览
     *
     * @param
     * @return cn.yuanqiao.common.core.domain.R<cn.yuanqiao.common.core.domain.entity.SysFile>
     * @author hbWang
     * @date 2023/9/15 16:26
     */
    @Override
    public byte[] queryImage(String id) {
        String tableCode = SecurityUtils.getTenantCode();
        String uplodPath = sysConfigService.selectConfigByKey("YQ_ZT_PATH");
        YqConsultRegister yqConsultRegister = yqConsultRegisterMapper.selectYqConsultRegisterById(id, new YqConsultRegister().getTableName());
        uplodPath = uplodPath + yqConsultRegister.getIntroduceLetter();

        return readFile(uplodPath);
    }
    /**
     * 读取文件为字节数据
     *
     * @param url 地址
     * @return 字节数据
     */
    private static byte[] readFile(String url) {
        InputStream in = null;
        ByteArrayOutputStream baos = null;
        try {
            // 本机地址
            in = new FileInputStream(url);

            return IOUtils.toByteArray(in);
        } catch (
                Exception e) {

            return null;
        } finally {
            IOUtils.closeQuietly(baos);
        }
    }

}
